<?php
class User extends AppModel {
	public $name = 'User';
	public $tablePrefix = 'cakeforum_';

	public $hasMany = array('Notification',
		'Thread' => array(
				'className' => 'Thread',
				'order' => 'id DESC',
				'limit' => 4
		),
		'Message'
	);

	public $validate = array(
		'username' => array(
			'required1' => array(
				'rule' => 'notEmpty',
				'message' => 'Please enter a username'
			),
			'required2' => array(
				'rule' => 'isUnique',
				'message' => 'That username already exists'
			),
			'required3' => array(
				'rule' => 'alphaNumeric',
				'message' => 'Usernames must only contain letters and numbers'
			),
			'required4' => array(
				'rule' => array('minLength', 4),
				'message' => 'Usernames be at least 4 characters long'
			),
			'required5' => array(
				'rule' => array('maxLength', 30),
				'message' => 'Usernames must be no larger than 30 characters long'
			)
		),
		'email' => array(
			'required3' => array(
				'rule' => 'notEmpty',
				'message' => 'Please enter an email address'
			),
			'required4' => array(
				'rule' => 'email',
				'message' => 'Please enter a valid email'
			),
			'required5' => array(
				'rule' => 'isUnique',
				'message' => 'This email address has already been registered'
			)
		),
		'password' => array(
			'required' => array(
				'rule' => array('minLength', 6),
				'message' => 'Password must be 6 characters are more'
			)
		),
		'password1' => array(
			'required6' => array(
				'rule' => array('minLength', 6),
				'message' => 'Password must be 6 characters are more'
			),
			'passwordcheck' => array(
				'rule' => 'comparePasswords',
				'message' => 'Passwords don\'t match'
			)
		),
		'password2' => array(
			'passwordcheck2' => array(
				'rule' => array('minLength', 6),
				'message' => '',

			),
			'passwordcheck3' => array(
				'rule' => 'comparePasswords',
				'message' => 'Passwords don\'t match'
			)
		),
		'forgotusername' => array(
			'required1' => array(
				'rule' => 'notEmpty',
				'message' => 'Please enter your username'
			),
			'required2' => array(
				'rule' => array('userExists'),
				'message' => 'This user doesn\'t exist'
			)
		),
		'resetpassword1' => array(
			'required1' => array(
				'rule' => array('minLength', 6),
				'message' => 'Password must be 6 characters or more'
			),
			'passwordcheck' => array(
					'rule' => 'resetComparePasswords',
					'message' => 'Passwords don\'t match'
			)
		),
		'resetpassword2' => array(
			'required1' => array(
				'rule' => array('minLength', 6),
				'message' => ''
			),
			'passwordcheck' => array(
				'rule' => 'resetComparePasswords',
				'message' => 'Passwords don\'t match'
			)
		)
	);

	public function comparePasswords($check){
		return ($this->data[$this->alias]['password1'] === $this->data[$this->alias]['password2']) ? true : false;
	}

	public function resetComparePasswords($check){
		return ($this->data[$this->alias]['resetpassword1'] === $this->data[$this->alias]['resetpassword2']) ? true : false;
	}

	public function userExists($username){
		return ($this->find('count', array("conditions" => array("User.username" => $username))) > 0) ? true : false;
	}

	public function savePassword(){
		if($this->validates(array('fieldList' => array('resetpassword1', 'resetpassword2')))){
			$this->saveField('password', AuthComponent::password($this->data[$this->alias]['resetpassword1']));
			$this->saveField('token', '');
			$this->saveField('logintries', 0);
			$this->saveField('lockcode', null);
			return true;
		}
		return false;
	}


	public function sendWelcomeMessage(){
		// send welcome message
		$this->Message->set('user_id', $this->id);
		$this->Message->set('to_user_id', $this->id);
		$this->Message->set('from_user_id', 1);
		$this->Message->set('title', 'Welcome');
		$this->Message->set('body', 'Welcome to ' . SITE_NAME . ' If you have any questions feel free to contact me or post on the forums. - Chris');
		$this->Message->save();
	}

	public function unlock($lockcode){
		/* If passed $lockcode matches lockcode on users account
		 * remove lockcode and reset logintries to 0
		 */
		$userData = $this->read();
		if($userData[$this->alias]['lockcode'] == $lockcode){
			$this->saveField('logintries', 0);
			$this->saveField('lockcode', null);
			return true;
		}
		return false;
	}

	public function beforeSave() {
		if(isset($this->data[$this->alias]['password1'])){
			$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password1']);
		}
		if(isset($this->data[$this->alias]['reset_avatar'])) {
			$this->data[$this->alias]['user_file_path'] = 'default.jpg';
		}
		return true;
	}

}